package com.cskaoyan.controller.user;

import com.cskaoyan.bean.MallBrand;
import com.cskaoyan.model.bo.PageBo;
import com.cskaoyan.model.bo.user.BrandCreateBo;
import com.cskaoyan.model.vo.BaseRespVo;
import com.cskaoyan.model.vo.user.BrandListVo;
import com.cskaoyan.service.user.BrandService;
import com.cskaoyan.utils.RegularUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @program: project-market
 * @description:
 * @author: zhijie
 * @create: 2021-11-30 22:45
 **/
@RestController
@RequestMapping("admin/brand")
public class BrandController {
    @Autowired
    BrandService brandService;



    /**
     * 根据id跟name查询品牌信息
     *
     * @param id
     * @param name
     * @param pageBo
     * @return
     */
    @RequiresPermissions("admin:brand:list")
    @GetMapping("list")
    public BaseRespVo brandList(String id, String name, PageBo pageBo) {
        if (id != null && !"".equals(id)) {
            if (RegularUtil.isNumber(id) == false) {
                BaseRespVo fail = BaseRespVo.fail("参数不正确");
                fail.setErrno(402);
                return fail;
            }
        }
        BrandListVo data = brandService.brandList(id, name, pageBo);
        return BaseRespVo.ok(data);
    }

    /**
     * 添加品牌制造商
     *
     * @param bo
     * @param bindingResult
     * @return
     */
    @RequiresPermissions("admin:brand:create")
    @PostMapping("create")
    public BaseRespVo brandCreate( @RequestBody @Valid BrandCreateBo bo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            BaseRespVo fail = BaseRespVo.fail("参数不对");
            fail.setErrno(401);
            return fail;
        }
        MallBrand data = brandService.brandCreate(bo);
        if (data == null) {
            return BaseRespVo.fail("服务器繁忙");
        }
        return BaseRespVo.ok(data);
    }

    /**
     * 修改品牌商的信息
     *
     * @param
     * @param bindingResult
     * @param mallBrand
     * @return
     */
    @RequiresPermissions("admin:brand:update")
    @PostMapping("update")
    public BaseRespVo brandUpdate( @RequestBody @Valid MallBrand mallBrand, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            BaseRespVo fail = BaseRespVo.fail("参数不对");
            fail.setErrno(401);
            return fail;
        }
        MallBrand data = brandService.brandUpdate(mallBrand);
        if (data == null) {
            return BaseRespVo.fail("服务器繁忙");
        }
        return BaseRespVo.ok(data);
    }

    /**
     * 逻辑删除该品牌商数据
     *
     * @param mallBrand
     * @return
     */
    @RequiresPermissions("admin:brand:delete")
    @PostMapping("delete")
    public BaseRespVo brandDelete(@RequestBody MallBrand mallBrand) {
        brandService.brandDelete(mallBrand);
        return BaseRespVo.ok();

    }

}
